//入口文件
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

//时间插件
import moment from 'moment';
Vue.filter('dateForm',function(datastr,pattern='YYYY-MM-DD'){
    return moment(datastr).format(pattern);
});
Vue.filter('commentForm',function(datastr){
    if(datastr=='undefined'){
        datastr = '此用户很懒什么都诶有说';
    }
    return datastr;
})

import VueResource from 'vue-resource'
Vue.use(VueResource);
Vue.http.options.emulateJSON = true;

import Vuex from 'vuex'
Vue.use(Vuex);
//每次刚进入网站的时候就调用localStorage里面的数据，先初始化购物车的数据。
var car  = JSON.parse(localStorage.getItem('car') || '[]');
var store = new Vuex.Store({
    state:{  //this.$state.***
        car:car,//购物车里面商品的数据，一个商品是一个对象，含有名称和数量[{id:商品id,title:'',count:,price:商品价格,selectedFlag:商品是否选中被结算},{}]
    },
    mutations:{  //this.$store.commit("***","参数")
        addToCar(state,goodsCar){
            //使用mutations将数据更新
            //1，如果购物车中已经有了这个商品，只需要更新数量
            //2,如果购物车里面没有就将数据全部推入car[]

            var flag = false;//一开始假设该商品是全新的商品，购物车中之前是不存在的
            state.car.some(item=>{
                if(item.id == goodsCar.id) {
                    item.count += parseInt(goodsCar.count);
                    item.count = parseInt(item.count);
                    flag = true;
                    return true;  //找到了提前结束
                }  
            })
            if(!flag) 
                state.car.push(goodsCar);
            //当car被更新之后，存放在本地的localStroage里面
            localStorage.setItem('car',JSON.stringify(state.car));
        },
        updateCar(state,obj){
            state.car.some(item =>{
                if(item.id == obj.id){
                    item.count = parseInt(obj.count);
                    item.count = parseInt(item.count)
                    return true;
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car));
        },
        removeFromCar(state,id){
            state.car.some((item,i) =>{
                if(item.id == id){
                    //删除
                    state.car.splice(i);
                    return true;
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car));
        },
        updateSelectFlag(state,info){
            state.car.some(item =>{
                if(item.id == info.id){
                    item.selectFlag = info.Flag;
                    return true;
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car));
        }
    },
    getters:{  //this.$store.getters.***
        //只要数据改变了就调用
        getAllCount(state){
            var c = 0;
            state.car.forEach(item =>{
                c += parseInt(item.count);
            })
            return c;
        },
        getEveryCount(state){
            var o = {};
            state.car.forEach(item =>{
                o[item.id] = item.count;
            })
            return o;
        },
        getGoodsSelected(state){
            var e = {};
            state.car.forEach(item =>{
                e[item.id] = item.selectFlag;
            })
            return e;
        },
        getCheckInfo(state){
            var a = {
                count:0,
                amount:0
            }
            state.car.forEach(item=>{
                if(item.selectFlag){
                    a.count += item.count;
                    a.amount += item.count*item.price;
                }
            })
            return a;
        }
    }   
})

// import { Header,Swipe, SwipeItem,Button, Lazyload  } from 'mint-ui';
// Vue.component(Header.name, Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);
import mintUI from 'mint-ui';
Vue.use(mintUI);
import 'mint-ui/lib/style.css'

//图片查看的插件
import Viewer from 'v-viewer'
import 'viewerjs/dist/viewer.css'
Vue.use(Viewer)
Viewer.setDefaults({
    Options: { 'inline': true, 'button': true, 'navbar': true, 'title': true, 'toolbar': true, 'tooltip': true, 'movable': true, 'zoomable': true, 'rotatable': true, 'scalable': true, 'transition': true, 'fullscreen': true, 'keyboard': true, 'url': 'data-source' }
})

// import mui  from './lib/mui/dist/js/mui.min.js'
// Vue.prototype.mui = mui;
import './lib/mui/dist/css/mui.min.css'
import './lib/mui/dist/css/icons-extra.css'
import './lib/mui/dist/fonts/mui-icons-extra.ttf'

import router from './router.js'

import app from './App.vue'

var vm = new Vue({
    el:'#app',
    render: c => c(app),
    router,
    store  //将仓库挂载在vue上
})